<template>
  <div class="login-container">
    <div class="login-card">
      <div class="login-header">
        <h1 class="login-title">大明湖旅游系统</h1>
        <p class="login-subtitle">欢迎使用大明湖旅游信息管理系统</p>
      </div>

      <el-tabs v-model="activeTab" class="login-tabs">
        <el-tab-pane label="管理员登录" name="admin">
          <div class="tab-content">
            <el-form :model="adminForm" :rules="adminRules" ref="adminFormRef" class="login-form">
              <el-form-item prop="username">
                <el-input
                  v-model="adminForm.username"
                  placeholder="请输入用户名"
                  :prefix-icon="User"
                  @keyup.enter="handleAdminLogin"
                />
              </el-form-item>
              <el-form-item prop="password">
                <el-input
                  v-model="adminForm.password"
                  type="password"
                  placeholder="请输入密码"
                  :prefix-icon="Lock"
                  show-password
                  @keyup.enter="handleAdminLogin"
                />
              </el-form-item>
              <el-form-item>
                <el-button
                  type="primary"
                  class="login-button"
                  :loading="loading"
                  @click="handleAdminLogin"
                >
                  登录
                </el-button>
              </el-form-item>
              <div class="login-tips">
                <el-alert
                  v-if="loginError"
                  :title="loginErrorMessage"
                  type="error"
                  :closable="true"
                  @close="loginError = false"
                  show-icon
                />
                <p class="admin-hint">管理员账号: admin / 密码: admin123</p>
              </div>
            </el-form>
            <div class="admin-features">
              <h4>管理员功能:</h4>
              <ul>
                <li>景点信息管理</li>
                <li>系统数据维护</li>
                <li>景点位置编辑</li>
              </ul>
            </div>
          </div>
        </el-tab-pane>

        <el-tab-pane label="游客访问" name="visitor">
          <div class="tab-content">
            <el-form :model="visitorForm" :rules="visitorRules" ref="visitorFormRef" class="login-form">
              <el-form-item prop="name">
                <el-input
                  v-model="visitorForm.name"
                  placeholder="请输入您的名字（选填）"
                  :prefix-icon="User"
                  @keyup.enter="handleVisitorLogin"
                />
              </el-form-item>
              <el-form-item>
                <el-button
                  type="success"
                  class="login-button"
                  :loading="loading"
                  @click="handleVisitorLogin"
                >
                  开始游览
                </el-button>
              </el-form-item>
              <div class="visitor-features">
                <h4>游客功能:</h4>
                <ul>
                  <li>浏览景点信息</li>
                  <li>规划旅游路线</li>
                  <li>查看景点介绍</li>
                </ul>
              </div>
            </el-form>
          </div>
        </el-tab-pane>
      </el-tabs>

      <div class="login-footer">
        <p>© {{ currentYear }} 大明湖旅游信息管理系统</p>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, type FormInstance } from 'element-plus'
import { User, Lock, Plus } from '@element-plus/icons-vue'
import { useUserStore } from '@/stores/user'

const router = useRouter()
const userStore = useUserStore()

// 表单引用
const adminFormRef = ref<FormInstance>()
const visitorFormRef = ref<FormInstance>()

// 当前激活的标签页
const activeTab = ref('admin')

// 加载状态
const loading = ref(false)

// 登录错误状态
const loginError = ref(false)
const loginErrorMessage = ref('用户名或密码错误')

// 当前年份
const currentYear = computed(() => new Date().getFullYear())

// 管理员表单数据
const adminForm = ref({
  username: '',
  password: ''
})

// 游客表单数据
const visitorForm = ref({
  name: ''
})

// 管理员表单验证规则
const adminRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
  ]
}

// 游客表单验证规则
const visitorRules = {
  name: [
    { max: 20, message: '长度不能超过 20 个字符', trigger: 'blur' }
  ]
}

// 检查是否已登录，如果已登录则重定向到相应页面
onMounted(() => {
  if (userStore.isLoggedIn) {
    if (userStore.isAdmin()) {
      router.push('/admin')
    } else if (userStore.isVisitor()) {
      router.push('/visitor')
    }
  }
})

// 管理员登录处理
const handleAdminLogin = async () => {
  if (!adminFormRef.value) return

  await adminFormRef.value.validate((valid) => {
    if (valid) {
      loading.value = true
      loginError.value = false

      try {
        const loginSuccess = userStore.loginAsAdmin(
          adminForm.value.username,
          adminForm.value.password
        )

        if (loginSuccess) {
          ElMessage.success('登录成功，正在进入管理系统...')
          setTimeout(() => {
            router.push('/admin')
          }, 1000)
        } else {
          loginError.value = true
          loginErrorMessage.value = '用户名或密码错误'
          ElMessage.error('用户名或密码错误')
        }
      } finally {
        loading.value = false
      }
    }
  })
}

// 游客登录处理
const handleVisitorLogin = async () => {
  if (!visitorFormRef.value) return

  await visitorFormRef.value.validate((valid) => {
    if (valid) {
      loading.value = true

      try {
        userStore.loginAsVisitor(visitorForm.value.name)
        ElMessage.success('欢迎访问大明湖旅游系统')
        setTimeout(() => {
          router.push('/visitor')
        }, 1000)
      } finally {
        loading.value = false
      }
    }
  })
}
</script>

<style scoped>
.login-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
}

.login-card {
  width: 450px;
  padding: 35px;
  border-radius: 12px;
  background-color: white;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-title {
  font-size: 26px;
  color: #303133;
  margin-bottom: 10px;
  font-weight: 600;
}

.login-subtitle {
  font-size: 14px;
  color: #909399;
}

.login-tabs {
  margin-bottom: 20px;
}

.tab-content {
  display: flex;
  flex-direction: column;
}

.login-form {
  margin-top: 20px;
}

.login-button {
  width: 100%;
  padding: 12px 0;
  font-size: 16px;
}

.login-tips {
  margin-top: 15px;
}

.admin-hint {
  font-size: 12px;
  color: #909399;
  text-align: center;
  margin-top: 10px;
}

.admin-features,
.visitor-features {
  margin-top: 20px;
  padding: 15px;
  background-color: #f8f9fa;
  border-radius: 8px;
}

.admin-features h4,
.visitor-features h4 {
  margin-top: 0;
  margin-bottom: 10px;
  color: #606266;
  font-weight: 500;
}

.admin-features ul,
.visitor-features ul {
  margin: 0;
  padding-left: 20px;
  color: #606266;
}

.admin-features li,
.visitor-features li {
  margin-bottom: 5px;
}

.login-footer {
  margin-top: 25px;
  text-align: center;
  font-size: 12px;
  color: #909399;
}

/* 添加一些动画效果 */
.login-card {
  animation: fadeIn 0.5s ease-in-out;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(-20px); }
  to { opacity: 1; transform: translateY(0); }
}
</style>
